//
//  GGBaseViewController.swift
//  SwiftWeico
//
//  Created by denjunjie1 on 15/12/15.
//  Copyright © 2015年 Gel. All rights reserved.
//

import UIKit

extension GGBaseViewController : GGVistorViewDelegate {

    func vistorViewLogin() {
        
        print("login")
        
        let login = GGLoginViewController()
        
        presentViewController(UINavigationController(rootViewController: login) , animated: true, completion: nil)
    }

}


class GGBaseViewController: UITableViewController {

    var userLogin = false
    
    override func loadView() {
        
        userLogin ? super.loadView() : setupVistorView()
        
    }

    private  func setupVistorView(){
    
            view = vistorView
        
        vistorView.delegate = self
        
        if self is GGHomeViewController {
        
            vistorView.rotationAnimation()
            
            NSNotificationCenter.defaultCenter().addObserver(self, selector: "didEnterBackground", name: UIApplicationDidEnterBackgroundNotification, object: nil)
            NSNotificationCenter.defaultCenter().addObserver(self, selector: "didBecomeAcitve", name: UIApplicationDidBecomeActiveNotification, object: nil)
        }else if self is GGMessageViewController{
        
            vistorView.setupVistorView("visitordiscover_image_message", msg: "登录后，别人评论你的微博，发给你的消息，都会在这里收到通知")
        }else if self is GGDiscoverViewController {
           
            vistorView.setupVistorView("visitordiscover_image_message", msg: "登录后，最新、最热微博尽在掌握，不再会与实事潮流擦肩而过")
        } else if self is GGProfileViewController {
           
            vistorView.setupVistorView("visitordiscover_image_profile", msg: "登录后，你的微博、相册、个人资料会显示在这里，展示给别人")
        }
        
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Register", style: UIBarButtonItemStyle.Done, target: self, action: nil)
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Login", style: UIBarButtonItemStyle.Done, target: self, action: "vistorViewLogin")
    
    }
    
    deinit {
    
        if self is GGHomeViewController{
        
            NSNotificationCenter.defaultCenter().removeObserver(self)
        }
    }
    
    func didEnterBackground () {
    
        vistorView.pauseAnimation()
    }
    
    func didBecomeAcitve () {
    
        vistorView.resumeAnimation()
    }
    
    private lazy var vistorView : GGVistorView = GGVistorView()
}
